package data.crawling;




import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import websphinx.HTMLParser;
import websphinx.Page;

/**
 * This class build TBox and ABox out of the html pages retrieved.
 * @author j55wu
 *
 */
public class Builder {

	public static final String GLOSSARY = "Glossary";
	public static final String CAMERASHORTNAME = "cam_short_name";
	public static final String CAMERANAME = "cam_model";
	public static final String CAMTYPE = "cam_type"; //e.g. slr
	public static final String USERREVIEW = "user_review";
	public static final String USERREVIEWNUM = "num_user_review";
	public static final String AMAZONUSERREVIEW = "amazon_review";
	public static final String AMAZONUSERREVIEWNUM = "num_amazon_review";
	//static final String REVIEW = "Star";
	public static final char REVIEWDELIMITER = '*';
	public static final String IMAGE = "Image";
	
	public static final String RELEASEDATE = "release_date";
	public static final boolean NULLALLOWED = true;
	public static final String NULL = "NULL"; //filler as values for some features
	//Defined entities
	public final static String CAM = "Digital_Camera";
	public final static String DLSR = "Digital_SLR";
	public final static String DC = "Compact_Digital_Camera";
	public final static String Hybrid = "Hybrid_Mirrorless_Camera";
	
	public final static String ResellerPrice = "Seller-Price";
	public final static String ResellerPriceInstance = "seller_price";
	public final static String Reseller = "Seller";
	public final static String hasResellerPrice = "hasSaleInfo";
	public static final String VENDORROLE = "hasSeller";
	public static final String VendorRate = "seller-avg-rating";
	public static final String VendorName = "seller-name";
	public static final String VendorNumRate = "seller-total-rates";
	public final static String Manufacturer = "Manufacturer";
	public final static String manUrl = "url";
	public final static String hasLoc = "isLocatedIn";
	public final static String hasMan = "hasManufacturer"; //applies to camera
	public final static String Location = "Location";
	
	public static final String REVIEWROLE = "hasReview";
	public static final String RESOLUTIONROLE = "hasResolution";
	public static final String LOWRESOLUTION = "hasOtherResolution";
	public static final String MAXRESOLUTION = "hasMaxResolution";
	public static final String XRESOLUTION = "x-dim";
	public static final String YRESOLUTION = "y-dim";
	public static final String PRICEVENDORINFO = "price_vendor_info";
	public static final String VALUEFEATURE = "value";
	public final static String SellerName = "seller-name";
	public static final String PRICEROLE = "hasPrice";
	public static final String PRICE = "price";

	public static final boolean manualTBox = false;
//	static final boolean buildTerm = false;
	public static final boolean buildAssertions = true;
	public static final boolean randomNumCamInstancePerSeller = false;
	public static final char spaceHolder = '_'; 
	public static final int maxCrawlDepth = 3; 
	public static int maxRandomInstancePerPriceModel = 1;
	public static final String fileName = "dpreview";
	
	public void parsePage(Page page){
		HTMLParser parser = new HTMLParser();
		try {
			parser.parse(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Builder(){
		
	}
	
	// makes a string into a concept-style string, e.g, Concept_Sample
	public static String stringToConcept(String s){
		String ret = s.trim();
		String[] ns= ret.split(" ");
		ret = "";
		for(String m:ns){
			char ft = m.charAt(0);
			m= m.substring(1);
			if(Character.isLowerCase(ft)){
				String l = Character.toString(Character.toUpperCase(ft));
				m = l+m;
			}else{
				m = Character.toString(ft) + m;
			}
			ret += m;
		}
		return ret;
	}
	

	
	//true: append to tbox, otherwise to abox
	public void writeStringToFile(String s, boolean tbox){
	    FileWriter fstream = null;
	    BufferedWriter out = null;
	    String ta = tbox? ".tbox":".abox";
		try {
			fstream = new FileWriter(Builder.fileName + ta, true);
	        out = new BufferedWriter(fstream);
			out.append(s);
			out.newLine();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	
}
